package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.AnswerMapper;
import com.jieshuibao.mapper.AnswerUserMapper;
import com.jieshuibao.mapper.CounselorMapper;
import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.mapper.RewardFlowJsbMapper;
import com.jieshuibao.mapper.RewardFlowMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Answer;
import com.jieshuibao.model.db.AnswerExample;
import com.jieshuibao.model.db.Counselor;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.util.MoneyRandom;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class AnswerService{
	
	@Resource
	private AnswerMapper answerMapper;
	@Resource
	private QuestionMapper questionMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private RewardFlowMapper rewardFlowMapper;
	@Resource
	private RewardFlowJsbMapper rewardFlowJsbMapper;
	@Resource
	private UserDealRecordMapper userDealRecordMapper;
	@Resource
	private AnswerUserMapper answerUserMapper;
	@Resource
	private CounselorMapper counselorMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	public void addAnswer(Answer answer) {
		answerMapper.insertSelective(answer);
	}
	
	public void delAnswerByUuid(String uuid) {
		answerMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateAnswer(Answer answer) {
		answerMapper.updateByPrimaryKeySelective(answer);
	}
	
	public Answer getAnswerByUuid(String uuid) {
		return answerMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Answer> getPageAnswer(AnswerExample answerExample) {
		return answerMapper.selectPage(answerExample);
	}
	
	public List<Answer> getByExample(AnswerExample answerExample) {
		return answerMapper.selectByExample(answerExample);
	}
	public int countAnswer(AnswerExample answerExample) {
		return answerMapper.countByExample(answerExample);
	}
	public void updateAnswerAndCounselor(Answer answer,Counselor counselor){
		if(answer!=null){
			answerMapper.updateByPrimaryKeySelective(answer);
		}
		if(counselor!=null){
			counselorMapper.updateByPrimaryKeySelective(counselor);
		}
	}
	public void updateAndAddRewardUserAnswerQuestion(String counselorUuid,Answer answer,Question question,int money){
		double subMoney = MoneyRandom
				.getSubMoney(money);
		UserDealRecord userDealRecord1 = new UserDealRecord();
		UserDealRecord userDealRecord2 = new UserDealRecord();
		String uuid1 = UUIDGenerator.getUUID();//用户收入
		String uuid2 = UUIDGenerator.getUUID();//公司支出
		userDealRecord1.setUuid(uuid1);
		userDealRecord2.setUuid(uuid2);
		Date date = new Date();
		userDealRecord1.setCreateDate(date);
		userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
		userDealRecord1.setPaySucTime(new Date());
		userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
		userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
		userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
		userDealRecord2.setCreateDate(date);
		userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
		userDealRecord2.setPaySucTime(date);
		userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
		userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
		userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
		userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
		if(counselorUuid!=null){
			Counselor counselor = counselorMapper.selectByPrimaryKey(counselorUuid);
			if(counselor!=null){
				
				Integer grabNum = counselor.getGrabNum();
				Integer grabScore = counselor.getGrabScore();
				Counselor counselor2 = new Counselor();
				counselor2.setUuid(counselorUuid);
				counselor2.setGrabNum(grabNum+1);
				counselor2.setGrabScore(grabScore+5);
				counselorMapper.updateByPrimaryKeySelective(counselor2);
			}
			
			
		}
		
		
		
		
		if(answer!=null){
			String phoneNumber = answer.getPhoneNumber();
			userDealRecord1.setUserId(phoneNumber);
			answerMapper.updateByPrimaryKeySelective(answer);
			if (money>0) {
				User user = new User();
				user.setBalance(subMoney);
				user.setPhoneNumber(phoneNumber);
				userExMapper.updateUserAdvisoryNum(user);
				
			}
		}
		if(question!=null){
			questionMapper.updateByPrimaryKeySelective(question);
			String uuid = question.getUuid();
			userDealRecord1.setBusinessNo(uuid);
			userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			userDealRecord2.setBusinessNo(uuid);
			userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			
			userDealRecord1.setSum(subMoney);
			userDealRecord2.setSum(subMoney);
			if (money>0) {
				userDealRecordMapper.insertSelective(userDealRecord1);
				userDealRecordMapper.insertSelective(userDealRecord2);
			}
			
		}
		
	}
	
	public void updateAndAddRewardUser(User user,double money,String uuid){
		
		if(user!=null){
			UserDealRecord userDealRecord1 = new UserDealRecord();
			UserDealRecord userDealRecord2 = new UserDealRecord();
			String uuid1 = UUIDGenerator.getUUID();//用户收入
			String uuid2 = UUIDGenerator.getUUID();//公司支出
			userDealRecord1.setUuid(uuid1);
			userDealRecord2.setUuid(uuid2);
			Date date = new Date();
			userDealRecord1.setCreateDate(date);
			userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
			userDealRecord1.setPaySucTime(new Date());
			userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
			userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			userDealRecord2.setCreateDate(date);
			userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
			userDealRecord2.setPaySucTime(date);
			userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
			userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			userDealRecord1.setBusinessNo(uuid);
			userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			userDealRecord2.setBusinessNo(uuid);
			userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			userDealRecord1.setSum(money);
			userDealRecord2.setSum(money);
			userDealRecord1.setUserId(user.getPhoneNumber());
			userMapper.updateByPrimaryKeySelective(user);
			userDealRecordMapper.insertSelective(userDealRecord1);
			userDealRecordMapper.insertSelective(userDealRecord2);
		}
		
		
	}

}
